<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.ddz.util.JDBCUtil" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="com.ddz.model.User" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/26
  Time: 14:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    System.out.println("获取分数排行榜");
    ResultSet resultSet;
    resultSet = JDBCUtil.executeQuery("select * from user ORDER BY score Desc LIMIT 100");
%>
<html>
<head>
    <title>Title</title>
    <style>
        /*这边的样式会改变Index.jsp里面的样式*/
        table {
            border-collapse: collapse; /* 合并边框 */
            width: 80%; /* 根据需要设置宽度 */
            margin: 0 auto; /* 让表格在页面中水平居中 */
            border-style: none; /* 去掉表格边框 */
        }

        th, td {
            padding: 8px; /* 设置内边距，让内容显示更舒适 */
            text-align: center; /* 让内容居中显示 */
        }

    </style>
</head>

<body>

    <div id="list_container">
        <%--<p>您的排名：${index}</p>--%>
        <table>
            <tr>
                <th>排名</th>
                <th>玩家账号</th>
                <th>玩家昵称</th>
                <th>积分</th>
            </tr>
            <%
                int index = 1; // 用于记录当前行数
                try {
                    User user = (User) session.getAttribute("User");
                    while (resultSet.next() && index <= 100) { // 使用while循环，条件是结果集还有下一行并且当前行数不超过100
                        String userId = resultSet.getString("userId");
                        String nickname = resultSet.getString("nickname");
                        String score = resultSet.getString("score");
                        // 获取玩家的排名
                        if (user != null){
                            if (userId.equals(user.getUserId())){
                                request.setAttribute("index",index);
                                request.setAttribute("score",score);
                            }
                        }
            %>
            <tr>
                <td><%=index%></td>
                <td><%=userId%></td>
                <td><%=nickname%></td>
                <td><%=score%></td>
            </tr>
            <%
                        index++; // 处理完一行数据后，行数加1，准备处理下一行
                    }
                } catch (SQLException e) {
                    System.out.println("查询异常！");
                }
            %>
        </table>
    </div>

<%--    增加自身排名的p标签--%>
    <script>
        var myDiv = document.getElementById("list_container");
        myDiv.innerHTML = "<p>积分：${score}</p>"+"<p>您的排名：${index}</p>" + myDiv.innerHTML;
    </script>

</body>
</html>
